<?php
$libdir = dirname(__FILE__);
require_once "{$libdir}/coreLib.php";
/**
 * implements a basic Tag. All other tag implementations should extend this
 * @author Cornel Borina
 *
 */ 
class htmlTag extends lib{
	protected $name = '';
	protected $atr = array();
	protected $style;
	protected $id;
	protected $beforeTags = array();
	protected $afterTags = array();
	protected $content = '';
	const BEFORE_CONTENT = true;
	const AFTER_CONTENT = false;
	public $closeTag = true;
	public $isHead = false;
	public static $nrTags = 0;
	public function __construct($name = 'span',$content = '',$id = null, $atrib = null){
		self::$nrTags++;
		if(is_null($id)){
			$id = 'element'.htmlTag::$nrTags;
		}
		if($this->checkName($name))
			$this->name = $name;
		else 
			$this->name = 'span';
		$this->id = $id;
		if(is_array($atrib)){
			foreach($atrib as $key=>$val){
				$this->atr[$key] = $val;
			}
		}else if(is_string($atrib)){
			$atributes = explode(";",$atrib);
			foreach($atributes as $atr){
				$part = explode('=',$atr);
				if($this->checkName($part[0]))
					$this->atr[$part[0]]=$part[1];
			}
		}
		$this->content = $content;
		$this->style = new CssRule();
		//End of constructor
	}
	
	function __clone(){
		self::$nrTags++;
		$this->id.=self::$nrTags;
	}
	/**
	 * Check if a string can pass as a HTML tag or atribute name
	 * @param string $name
	 */
	private function checkName($name){
		if(strpos($name, " ") !== false) return false;
		$good = false;
		if(preg_match("/[^a-z1-6]/i", $name) != 0) return false;
		else return true;
	}
	/**
	 * Get the tag name
	 */
	public function getName(){
		return $this->name;
	}
	/**
	 * Set the tag name.By default it is set to be <span>
	 * @param string $name
	 */
	public function setName($name='span'){
		if($this->checkName($name)){
			$this->name = $name;
		}
	}
	
	/**
	 * Add a attribute to a tag declaration
	 * @param string $name
	 * @param string $value
	 */
	public function addAtribute($name,$value=''){
		if($this->checkName($name))
			$this->atr[$name] = htmlentities($value);
	}
	/**
	 * Get the atributes array
	 */
	public function getAtributes(){
		return $this->atr;
	}
	
	/**
	 * Get a speccific atribute
	 * @param string $name
	 */
	public function getAtribute($name){
		return $this->atr[$name];
	}
	/**
	 * Unset a specific atribute
	 * @param string $name
	 */
	public function deleteAtribute($name){
		unset($this->atr[$name]);
	}
	/**
	 * Get the HTML content of a tag
	 */
	public function getContent(){
		return $this->content;
	}
	public function setContent($content = ''){
		$this->content = $content;
	}
	public function getId(){
		return $this->id;
	}
	public function setId($idname){
		if($this->checkName($idname))
			$this->id = $idname;
	}
	/**
	 * Add an inline tag
	 * @param htmlTag $tag
	 * @param bool $beforeContent
	 */
	public function addInlineTag(htmlTag $tag, $beforeContent = true){
		if(!$this->tagOK($tag)) return;
		if($beforeContent) $this->beforeTags[]=$tag;
		else $this->afterTags[]=$tag;
	}
	/**
	 * alias for this->addInlineTag
	 * @param htmlTag $tag
	 * @param bool $beforeContent
	 */
	public function addTag(htmlTag $tag,$beforeContent){
		$this->addInlineTag($tag,$beforeContent);
	}
	/**
	 * Check a tag to be suitable for adding. This should be overridden for the specific tag implementation
	 * @param htmlTag $tag
	 */
	protected function tagOK(htmlTag $tag){
		return true;
	}
	
	/**
	 * Set a style option for the curent element
	 * @param string $name
	 * @param string $value
	 */
	public function setCss($name,$value) {
		$this->style->addAtribute($name, $value);
	}
	/**
	 * Set a style and dump the previous
	 * @param CssRule $rule
	 */
	public function setStyle(CssRule $rule){
		$this->style = $rule;
	}
	/**
	 * convert the current element to a html code
	 * @see lib/lib::toHtml()
	 */
	public function toHtml($closeTag = null, $isHead = null) {
		if(!isset($closeTag) || is_null($closeTag)){
			$closeTag = $this->closeTag;
		}
		if(is_null($isHead)){
			$isHead = $this->isHead;
		}
		$html = "\n<";
		$html.=$this->getName();
		foreach($this->atr as $aname => $avalue){
			$html.= ' '.$aname.'="'.$avalue.'"';
		}
		//echo $this->getName();
		//var_dump($isHead,$closeTag);
		if(!$isHead){
			$html.= " id=\"{$this->id}\"";
			$html.=' style="';
			$html.=$this->style->loadAtributes(false);
			$html.='"';
		}
		if($closeTag){
			
			$html.='>';
			foreach($this->beforeTags as $tag)
				$html.=$tag->toHtml();
			$html.=$this->getContent();
			foreach($this->afterTags as $tag)
				$html.=$tag->toHtml();
			
			$html.='</'.$this->getName().'>';
		}else{
			$html.=' />';
		}
		$html.="\n";
		return $html;
	}
	//end of class
}